package com.hualan.sfzy.service.course.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.entity.CourseOrder;
import com.hualan.sfzy.mapper.course.CourseOrderMapper;
import com.hualan.sfzy.service.course.CourseOrderService;
import com.hualan.sfzy.vo.CourseOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class CourseOrderServiceImpl implements CourseOrderService {
    @Autowired
    private CourseOrderMapper courseOrderMapper;
    @Override
    public int add(CourseOrder entity) {
        return courseOrderMapper.insert(entity);
    }

    @Override
    public int edit(CourseOrder entity) {
        return courseOrderMapper.update(entity);
    }

    @Override
    public int remove(Integer id) {
        return courseOrderMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        System.out.println(ids);
        return courseOrderMapper.deleteBatch(ids);
    }

    @Override
    public List<CourseOrder> find() {
        return courseOrderMapper.selectAll();
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, CourseOrder entity) {
        PageHelper.startPage(pageNum,pageSize);

        List<CourseOrder> courseOrders = courseOrderMapper.select(entity);
        PageInfo<CourseOrder> pageInfo = new PageInfo<>(courseOrders);
        Map<String, Object> result = new HashMap<>();
        result.put("list", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        result.put("pageNum", pageInfo.getPageNum());
        result.put("pageSize", pageInfo.getPageSize());
        result.put("pages", pageInfo.getPages());
        return result;
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, CourseOrderVo entity) {
        PageHelper.startPage(pageNum,pageSize);

        List<CourseOrderVo> courseOrders = courseOrderMapper.selectVo(entity);
        PageInfo<CourseOrderVo> pageInfo = new PageInfo<>(courseOrders);
        Map<String, Object> result = new HashMap<>();
        result.put("list", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        result.put("pageNum", pageInfo.getPageNum());
        result.put("pageSize", pageInfo.getPageSize());
        result.put("pages", pageInfo.getPages());
        return result;
    }

    @Override
    public int addNewOrder(Integer courseId, Integer useId, double price) {
        System.out.println(useId);
        CourseOrder courseOrder = new CourseOrder();
        courseOrder.setCourseId(courseId);
        courseOrder.setUserId(useId);
        courseOrder.setTime(new Date());
        courseOrder.setActualPrice(BigDecimal.valueOf(price));
        courseOrder.setStatus("已购买");
        courseOrder.setDel(0);
        System.out.println(courseOrder);

        return courseOrderMapper.insert(courseOrder);
    }

    @Override
    public CourseOrder findById(Integer id) {
        return courseOrderMapper.selectById(id);
    }
}
